<?php
	require("../includes/setup.php");
	require(I_PATH."/fpdf17/fpdf.php");
	require(I_PATH."/pdfFunc.php");

	$pdf = new FPDF();
	
	$pdf->AddPage(); 
	$pdf->SetFont('Arial','',34);
	$pdf->Cell(40,7,"Liste des clients",0, 1);
	$pdf->Ln();
	$bd = openBD();
	$stmt = $bd->prepare("SELECT nom, prenom, mail, account FROM CLIENT");
	$stmt->setFetchMode(PDO::FETCH_ASSOC);
	$stmt->execute();
	// En-tête
	$pdf->SetFontSize(18);
	$header = array('Nom', 'Prénom', 'Mail', 'Compte (euros)');
    foreach($header as $col){
        $pdf->Cell(48,7,strDecode($col),1);
	}
	$pdf->Ln();
	$pdf->SetFontSize(12);
	while ($client = $stmt->fetch()) {
		foreach($client as $att){
			$pdf->Cell(48,7,strDecode($att),1);
		}
		$pdf->Ln();
	}
	
	$pdf->SetFont('Arial','',34);
	$pdf->Ln();
	$pdf->Ln();
	$pdf->Cell(40,7,"Stocks",0, 1);
	$pdf->Ln();
	require I_PATH . 'xml.php';
	$dvds = $dvd_dom->getElementsByTagName('dvds')->item(0);
	
	// En-tête
	$pdf->SetFontSize(20);
	$header = array('Nom', 'Copies totales', 'Copies restantes');
    foreach($header as $col){
        $pdf->Cell(64,7,strDecode($col),1);
	}
	$pdf->Ln();
	$pdf->SetFontSize(12);
	$stmt = $bd->prepare("SELECT COUNT(*) FROM CLIENT_has_DVD WHERE idDvd = :idDvd");
	foreach ($dvds->getElementsByTagName('dvd') as $dvd) {
		$id = $dvd->getAttribute("id");
		$stmt->bindParam(':idDvd', $id);
		$stmt->execute();
		$title = strDecode($dvd->getElementsByTagName('title')->item(0)->nodeValue);
		$i = 0;
		while($i < strlen($title)){
			if($pdf->getStringWidth(substr($title, 0, $i)) > 55){
				break;
			}
			$i++;
		}
		if($i < strlen($title))
			$title = substr($title, 0, $i) . "...";
		$pdf->Cell(64,7,$title,1);
		$pdf->Cell(64,7,strDecode($dvd->getElementsByTagName('dvdcount')->item(0)->nodeValue),1);
		$pdf->Cell(64,7,strDecode($dvd->getElementsByTagName('dvdcount')->item(0)->nodeValue - $stmt->fetchColumn()),1);
		$pdf->Ln();
	}
	$pdf->Output();
?>


<?php
	/*require("../includes/setup.php");
	require(I_PATH."/fpdf17/fpdf.php");

	class PDF extends FPDF
	{
		var $B;
		var $I;
		var $U;
		var $HREF;

		function PDF($orientation='P', $unit='mm', $size='A4')
		{
			// Appel au constructeur parent
			$this->FPDF($orientation,$unit,$size);
			// Initialisation
			$this->B = 0;
			$this->I = 0;
			$this->U = 0;
			$this->HREF = '';
		}

		function WriteHTML($html)
		{
			// Parseur HTML
			$html = str_replace("\n",' ',$html);
			$a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
			foreach($a as $i=>$e)
			{
				if($i%2==0)
				{
					// Texte
					if($this->HREF)
						$this->PutLink($this->HREF,$e);
					else
						$this->Write(5,$e);
				}
				else
				{
					// Balise
					if($e[0]=='/')
						$this->CloseTag(strtoupper(substr($e,1)));
					else
					{
						// Extraction des attributs
						$a2 = explode(' ',$e);
						$tag = strtoupper(array_shift($a2));
						$attr = array();
						foreach($a2 as $v)
						{
							if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
								$attr[strtoupper($a3[1])] = $a3[2];
						}
						$this->OpenTag($tag,$attr);
					}
				}
			}
		}

		function OpenTag($tag, $attr)
		{
			// Balise ouvrante
			if($tag=='B' || $tag=='I' || $tag=='U')
				$this->SetStyle($tag,true);
			if($tag=='A')
				$this->HREF = $attr['HREF'];
			if($tag=='BR')
				$this->Ln(5);
		}

		function CloseTag($tag)
		{
			// Balise fermante
			if($tag=='B' || $tag=='I' || $tag=='U')
				$this->SetStyle($tag,false);
			if($tag=='A')
				$this->HREF = '';
		}

		function SetStyle($tag, $enable)
		{
			// Modifie le style et sélectionne la police correspondante
			$this->$tag += ($enable ? 1 : -1);
			$style = '';
			foreach(array('B', 'I', 'U') as $s)
			{
				if($this->$s>0)
					$style .= $s;
			}
			$this->SetFont('',$style);
		}

		function PutLink($URL, $txt)
		{
			// Place un hyperlien
			$this->SetTextColor(0,0,255);
			$this->SetStyle('U',true);
			$this->Write(5,$txt,$URL);
			$this->SetStyle('U',false);
			$this->SetTextColor(0);
		}
	}

	require I_PATH . 'xml.php';
	$global_dom = loadDom(X_PATH . 'general.xsl');
	$html = transform($dvd_dom, $global_dom)->saveXML();

	$pdf = new PDF();
	// Première page
	$pdf->AddPage();
	$pdf->SetFont('Arial','',20);
	$pdf->SetFontSize(14);
	$pdf->WriteHTML($html);
	$pdf->Output();*/
?>
